﻿using System;
using System.Collections.Generic;
using System.Web;

using LitJson;

//namespace WebApplication2 {
	public abstract class Handler {

		public HttpRequest Request { get; private set; }
    public HttpResponse Response { get; private set; }
    public HttpContext Context { get; private set; }
    public HttpServerUtility Server { get; private set; }

		public Handler(HttpContext context) {
			this.Request = context.Request;
			this.Response = context.Response;
			this.Context = context;
			this.Server = context.Server;
		}

		public abstract void Process();

		protected void WriteJson(JsonData response) {
			string jsonpCallback = Request["callback"], json = JsonMapper.ToJson(response);
			if (String.IsNullOrEmpty(jsonpCallback)) {
				Response.AddHeader("Content-Type", "text/plain");
				Response.Write(json);
			} else {
				Response.AddHeader("Content-Type", "application/javascript");
				Response.Write(String.Format("{0}({1});", jsonpCallback, json));
			}
			Response.End();
		}
	}
//}
